package gui;



import javax.swing.JPanel;
import java.awt.Frame;

import javax.swing.JDialog;
import java.awt.Dimension;
import javax.swing.WindowConstants;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.JList;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Point;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;

import negocios.Fachada;
import negocios.Operacoes;
import negocios.Produto;

public class CadastroDeFTO3 extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JScrollPane jScrollPaneOperAcab = null;
	private JList lstOperAcab = null;
	private JButton badd = null;
	private JButton bretirar = null;
	private JButton bFinish = null;
	private JLabel lEtapa = null;
	private JScrollPane jScrollPaneOperAcabRecebe = null;
	private JList lstOperAcabRecebe = null;
	private Operacoes[] operacoes = null;
	private Operacoes[] opPreparacao = null;
	private Operacoes[] opMontagem = null;
	private Produto prod = null;
	/**
	 * @param owner
	 */
	public CadastroDeFTO3(Frame owner,Produto prod,Operacoes[] opPreparacao,Operacoes[] opMontagem) {
		super(owner);
		initialize();
		this.opPreparacao = opPreparacao;
		this.opMontagem  = opMontagem;
		this.prod = prod;		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(523, 321);
		this.setTitle("SIG CONFEX - CADASTRO DE FICHA TECNICA OPERACIONAL");
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lEtapa = new JLabel();
			lEtapa.setBounds(new Rectangle(187, 29, 119, 26));
			lEtapa.setFont(new Font("Dialog", Font.BOLD, 18));
			lEtapa.setText("Acabamento:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJScrollPaneOperAcab(), null);
			jContentPane.add(getBadd(), null);
			jContentPane.add(getBretirar(), null);
			jContentPane.add(getBFinish(), null);
			jContentPane.add(lEtapa, null);
			jContentPane.add(getJScrollPaneOperAcabRecebe(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPaneOperAcab	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneOperAcab() {
		if (jScrollPaneOperAcab == null) {
			jScrollPaneOperAcab = new JScrollPane();
			jScrollPaneOperAcab.setBounds(new Rectangle(5, 76, 193, 174));
			jScrollPaneOperAcab.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jScrollPaneOperAcab.setViewportView(getLstOperAcab());
		}
		return jScrollPaneOperAcab;
	}

	/**
	 * This method initializes lstOperAcab	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getLstOperAcab() {
		if (lstOperAcab == null) {
			lstOperAcab = new JList();
			lstOperAcab.setVisibleRowCount(5);
			lstOperAcab.setBounds(new Rectangle(0, 0, 158, 153));
			lstOperAcab.setToolTipText("Selecione aqui as operacoes presentes neste produto.");
			lstOperAcab.setBackground(Color.white);
			lstOperAcab.setModel(new DefaultListModel());
			((DefaultListModel) (lstOperAcab.getModel())).clear();			
			operacoes = Fachada.getInstancia().listarOperacao(null, "listar");
			for (int i = 0; i < operacoes.length; i++) {
		
				((DefaultListModel) (lstOperAcab.getModel())).addElement(operacoes[i].getDescricao());
			
			}
		}
		return lstOperAcab;
	}

	/**
	 * This method initializes badd	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBadd() {
		if (badd == null) {
			badd = new JButton();
			badd.setBounds(new Rectangle(208, 113, 86, 20));
			badd.setToolTipText("Clique aqui para adicionar a/as operacoes selecionadas.");
			badd.setText(">>");
			badd.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//lstOperPrepRecebe.setListData(lstOperPrep.getSelectedValues());
					Object[] ob = lstOperAcab.getSelectedValues();	
					int[] indexes = lstOperAcab.getSelectedIndices();
					for(int i=0;i<ob.length;i++){
					((DefaultListModel) (lstOperAcabRecebe.getModel())).addElement(ob[i]);
					((DefaultListModel) (lstOperAcab.getModel())).removeElementAt(indexes[i]);
					}
				}
			});
		}
		return badd;
	}

	/**
	 * This method initializes bretirar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBretirar() {
		if (bretirar == null) {
			bretirar = new JButton();
			bretirar.setText("<<");
			bretirar.setSize(new Dimension(86, 20));
			bretirar.setToolTipText("Clique aqui para remover operacoes colocadas indevidamente.");
			bretirar.setLocation(new Point(208, 170));
			bretirar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					((DefaultListModel) (lstOperAcab.getModel())).addElement(lstOperAcabRecebe.getSelectedValue());
					((DefaultListModel) (lstOperAcabRecebe.getModel())).removeElement(lstOperAcabRecebe.getSelectedValue());
					
				}
			});
		}
		return bretirar;
	}

	/**
	 * This method initializes bFinish	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBFinish() {
		if (bFinish == null) {
			bFinish = new JButton();
			bFinish.setBounds(new Rectangle(406, 262, 93, 20));
			bFinish.setToolTipText("Clique aqui para ir para a proxima etapa da ficha.");
			bFinish.setText("Finish");
			bFinish.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//Pega os items da segunda JList
					Object[] opers = ((DefaultListModel) (lstOperAcabRecebe.getModel())).toArray();
					Operacoes[] ops = new Operacoes[opers.length];
					//Converte os elementos da JList em um Array de Opera��es;
					for(int i = 0;i<opers.length;i++){
						Operacoes o = new Operacoes();
						o.setDescricao(opers[i].toString());
						ops[i] = Fachada.getInstancia().ConsultarOperacao(o, false);
					}
					FinishFTO fim = new FinishFTO(null,prod,opPreparacao,opMontagem,ops);
					dispose();
					fim.setVisible(true);
					
					
				}
			});
		}
		return bFinish;
	}

	/**
	 * This method initializes jScrollPaneOperAcabRecebe	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneOperAcabRecebe() {
		if (jScrollPaneOperAcabRecebe == null) {
			jScrollPaneOperAcabRecebe = new JScrollPane();
			jScrollPaneOperAcabRecebe.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jScrollPaneOperAcabRecebe.setLocation(new Point(312, 75));
			jScrollPaneOperAcabRecebe.setSize(new Dimension(193, 174));
			jScrollPaneOperAcabRecebe.setViewportView(getLstOperAcabRecebe());
		}
		return jScrollPaneOperAcabRecebe;
	}

	/**
	 * This method initializes lstOperAcabRecebe	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getLstOperAcabRecebe() {
		if (lstOperAcabRecebe == null) {
			lstOperAcabRecebe = new JList();
			lstOperAcabRecebe.setSize(new Dimension(160, 155));
			lstOperAcabRecebe.setToolTipText("Retire alguma operacao, caso tenha a colocado indevidamente.");
			lstOperAcabRecebe.setModel(new DefaultListModel());
			lstOperAcabRecebe.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			lstOperAcabRecebe.setVisibleRowCount(5);
			lstOperAcabRecebe.setBackground(Color.white);
		}
		return lstOperAcabRecebe;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
